<template>
	<view>
		<view id="comment-box" class="comment-box">
			<CommentList :propsList="list" :page="page" :imagesToPreview="imagesToPreview" :propsTopic="topic" :propsReport="report" v-on:fresh-page="freshPage"></CommentList>
		</view>
		<!-- <BottomAction></BottomAction> -->
		<TextInput :propsTextInputPlaceholder="textInputPlaceholder" :page="page" v-on:direct-comment="directComment" v-on:text-input-tab-image="gotoCreate"></TextInput>
	</view>
</template>

<script>
	import CommentList from '../../components/comment-list.vue'
	import ListOptions from '../../minxins/list-options.js'
	import TextInput from '../../components/text-input.vue'
	import CommentCreate from '../../minxins/comment-create.js'
	import BottomAction from '../../components/bottom-action.vue'
	export default {
		data() {
			return {
				page: 'topic-comment',
				imagesToPreview: {},
				textInputPlaceholder: "回复楼主",
				topic: null,
				report: null
			}
		},
		mixins: [ListOptions, CommentCreate],
		components: {
			CommentList,
			TextInput,
			BottomAction
		},
		async onLoad(options) {
			this.options = Object.assign({}, this.options, options)
			console.log(options)
			let result = await this.fetchPageData()
			uni.setNavigationBarTitle({
				title: `帖子详情`
			})
			this.renderPage(result)
			if (options.notification_id && (options.read_at == 'null' || options.read_at == 'undefined')) {
				this.$api.readNotification({__resource_id: options.notification_id})
			}
			if (result.data.topic) {
				let topic = result.data.topic
				this.topic = topic
				this.report = result.data.report
				
				// #ifdef MP
				//注册分享按钮
				console.log('register share', result)
				uni.showShareMenu({
					title: 'title: ' + topic.title_excerpt,
					content: 'content: ' + topic.title_excerpt,
					path: '/pages/topic-comment/topic-comment?tid=' + topic.id
				})
				// #endif
			}
		},
		methods: {
			async fetchPageData() {
				let params = this.options;
				params.__resource_id = params.tid;
				params.pid = 0
				params.include_is_liked = 1
				let result = await this.$api.showTopic(params)
				if (this.options.page == 1) {
					this.imagesToPreview = {}
				}
				result.data.data.map(comment => {
					let key = comment.key;
					comment.detail.content.map(content => {
					  if (content.type == "image") {
						if (!this.imagesToPreview[key]) {
						  this.imagesToPreview[key] = [];
						}
						this.imagesToPreview[key].push(content.data.url)
					  }
					})
					if (comment.hide) {
						comment.hide.content.map(content => {
						  if (content.type == "image") {
							if (!this.imagesToPreview[key]) {
							  this.imagesToPreview[key] = [];
							}
							this.imagesToPreview[key].push(content.data.url)
						  }
						})
					}
				})
				return result;
			},
			gotoCreate(e) {
				uni.navigateTo({
				    url: "/pages/create/create?title=回复楼主&tid=" + this.options.tid + "&action=create_comment"
				})
			}
		},
		events: {
			
		},
		onShow() {
			this.checkNewComment()
		}
		
	}
</script>

<style lang="scss">
	.comment-box {
		padding-bottom: 100upx;
	}
</style>
